package com.changgou.service.goods.service.impl;

import com.changgou.goods.pojo.Brand;
import com.changgou.goods.pojo.Spec;
import com.changgou.service.goods.dao.BrandMapper;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;



    /**
     * 品牌列表查询
     * @return
     */
    @Override
    public List<Brand> findBrandList() {
        return brandMapper.selectAll();
    }




    /*
        根据id查询品牌
         */
    @Override
    public Brand findById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }


    /**
     * 添加品牌
     * @param brand
     */
    @Override
    @Transactional
    public void addBrand(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    /**
     * 根据id修改品牌
     * @param brand
     */
    @Override
    public void updateBrand(Brand brand) {
        brandMapper.updateByPrimaryKey(brand);
    }

    /**
     * 根据id删除品牌信息
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    /**
     * 品牌列表的条件查询
     * @param brand
     * @return
     */
    @Override
    public List<Brand> search(Brand brand) {
        Example example = new Example(Brand.class);
        //封装查询条件
        Example.Criteria criteria = example.createCriteria();

        if (brand != null) {
            //品牌名称(模糊查询)like %
            if (brand.getName()!=null && !"".equals(brand.getName())){
                criteria.andLike("name","%"+brand.getName()+"%");
            }

            //按照品牌首字母进行查询(精确)
            if (brand.getLetter() != null && !"".equals(brand.getLetter())) {
                criteria.andLike("letter",brand.getLetter());
            }

        }


        return brandMapper.selectByExample(example);
    }

    @Override
    public com.github.pagehelper.Page<Brand> findPage(Brand brand, Integer page, Integer size) {
        PageHelper.startPage(page, size);

        //设置查询条件
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();

        if (brand != null) {
            //品牌名称(模糊查询)like %
            if (brand.getName()!=null && !"".equals(brand.getName())){
                criteria.andLike("name","%"+brand.getName()+"%");
            }

            //按照品牌首字母进行查询(精确)
            if (brand.getLetter() != null && !"".equals(brand.getLetter())) {
                criteria.andLike("letter",brand.getLetter());
            }

        }

        com.github.pagehelper.Page<Brand> pageInfo = (Page<Brand>) brandMapper.selectByExample(example);
        return pageInfo;
    }

    @Override
    public Page<Brand> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        return (Page<Brand>)brandMapper.selectAll();
    }

    /**
     * 根据分类名称查找品牌列表
     * @param categoryName
     * @return
     */
    @Override
    public List<Brand> findBrandByCategoryName(String categoryName) {
        return brandMapper.findBrandByCategoryName(categoryName);
    }



}
